package com.example.shushiyunsst.fragment.main.home.child

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.bean.NewsListBean
import com.example.shushiyunsst.fragment.main.home.child.bean.NewsListChildListBean
import com.example.shushiyunsst.util.NEWS_LIST
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

/**
 * 资讯
 * */
@Route(path = NEWS_LIST)
class NewsListFragment : SimpleListFragment<NewsListBean>(), OnItemChildClickListener {

    companion object {
        fun newInstance(): NewsListFragment {
            val args = Bundle()
            val fragment = NewsListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
        isEnableLoadMore = false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        addHeader(R.layout.comment_header_layout)
        mHeader.setBackgroundResource(R.color.bg_color_0)
        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.title = "资讯"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mAdapter.addChildClickViewIds(R.id.headerLine)
        mAdapter.setOnItemChildClickListener(this)
        mRecyclerView.clipToPadding=false
        mRecyclerView.setPadding(0,0,0,QMUIDisplayHelper.dp2px(_mActivity,10))
    }

    override fun getItemLayout(): Int {
        return R.layout.news_list_item_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: NewsListBean?) {
        helper?.setImageNetUrl(R.id.headerLineCover, item?.imgUrl)
            ?.setText(R.id.headerLineTv, item?.title)
            ?.setText(R.id.timeTv, item?.createTime)

        val mAdapter = object : CommAdapter<NewsListChildListBean>(
            _mActivity,
            R.layout.news_list_sub_item_fragment
        ) {
            override fun convert(
                holder: XQuickViewHolder?,
                subItem: NewsListChildListBean?,
                position: Int
            ) {
                holder?.displayImage(R.id.childCover, subItem?.imgUrl)
                    ?.setText(R.id.childTitle, subItem?.title)
                    ?.setText(R.id.childDes, subItem?.contentDescShort)
                    ?.setVisible22(R.id.line, position < ((item?.childList?.size ?: 0) - 1))
                    ?.setOnClickListener(R.id.subItemArea){
                        start(NewsDetailFragment.newInstance("${subItem?.nid}"))
                    }
            }

        }
        val mLineLayout: XGridLayout? = helper?.findView22(R.id.mLineLayout)
        mLineLayout?.maxItem = item?.childList?.size ?: 0
        mLineLayout?.setAdapter(mAdapter, lifecycle)
        for (e in item?.childList ?: emptyList()) {
            mAdapter.add(e)
        }
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(NewsListBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        RestClient.builder()
            .url(R.string.queryIndexNews)
            .success {
                parseDate(it)
            }
            .error { _, _ -> onFailed() }
            .failure { onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val list = obj["list"].toString()
        val array = JSON.parseArray(list, NewsListBean::class.java)
        loadListDate(array)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = mAdapter.getItem(position)
        when(view.id){
            R.id.headerLine->{
                start(NewsDetailFragment.newInstance("${item.nid}"))
            }
        }
    }
}